package com.bsj.pay.controller;

import com.bsj.pay.common.Result;
import com.bsj.pay.config.GlobalConfig;
import com.bsj.pay.controller.admin.UserController;
import com.bsj.pay.model.MerchantBean;
import com.bsj.pay.model.MessageBean;
import com.bsj.pay.model.OrderBean;
import com.bsj.pay.service.MerchantService;
import com.bsj.pay.service.MessageService;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.util.HttpUtil;
import com.bsj.pay.util.QrCodeUtil;
import com.bsj.pay.util.date.DateTimeUtil;
import com.bsj.pay.util.http.Http;
import com.bsj.pay.util.http.Http.Response;
import com.bsj.pay.util.json.JSONUtil;
import com.bsj.pay.util.security.Base64;
import com.bsj.pay.util.security.MD5;
import com.bsj.pay.util.security.PassWrodsCreater;
import com.google.zxing.WriterException;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Controller
@RequestMapping("/api")
public class PayController {

    private static Logger log = LogManager.getLogger(PayController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private WxMpService wxMpService;

    public String deleteAllHTMLTag(final String source) {
        if (source == null) {
            return "";
        }
        String s = source;
        s = s.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
        s = s.replaceAll("&.{2,6}?;", "");
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/pay")
    public Result pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        Result result = new Result();
        try {
            String merchant_no = request.getParameter("merchant_no");
            String out_order_no = request.getParameter("out_order_no");
            String amount = request.getParameter("amount");
            String channel_type = request.getParameter("channel_type");
            String pay_type = request.getParameter("pay_type");
            String notify_url = request.getParameter("notify_url");
            String sign = request.getParameter("sign");
            if (StringUtils.isEmpty(merchant_no)) {
                result.setCode(101);
                result.setMsg("参数缺失merchant_no");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(out_order_no)) {
                result.setCode(102);
                result.setMsg("参数缺失out_order_no");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(amount)) {
                result.setCode(103);
                result.setMsg("参数错误amount");
                result.setData("");
                return result;
            }
            if (Integer.parseInt(amount) < 1) {
                result.setCode(104);
                result.setMsg("amount必须大于1");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(pay_type)) {
                result.setCode(105);
                result.setMsg("参数缺失pay_type");
                result.setData("");
                return result;
            }
            if (!"wechat".equals(pay_type) && !"wechatScan".equals(pay_type) && !"alipay".equals(pay_type) && !"alipaySdk".equals(pay_type)) {
                result.setCode(106);
                result.setMsg("pay_type只能为alipay或alipaySdk或wechat或wechatScan");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(notify_url)) {
                result.setCode(107);
                result.setMsg("参数缺失notify_url");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(sign)) {
                result.setCode(108);
                result.setMsg("参数缺失sign");
                result.setData("");
                return result;
            }
            if (StringUtils.isEmpty(channel_type)) {
                result.setCode(109);
                result.setMsg("参数缺失channel_type");
                result.setData("");
                return result;
            }
            // if (StringUtils.isEmpty(channel_type)) {
            // 	channel_type = "weidian";
            // }
            MerchantBean merchant = merchantService.getById(Long.parseLong(merchant_no));
            if (merchant == null) {
                result.setCode(201);
                result.setMsg("商户不存在");
                result.setData("");
                return result;
            }
            if (merchant.getStatus() == 0) {
                result.setCode(202);
                result.setMsg("商户被禁用");
                result.setData("");
                return result;
            }
            String signstr = merchant_no + out_order_no + amount + channel_type + pay_type + notify_url;
            log.info("signstr:" + signstr);
            String md5 = MD5.getMD5(signstr + merchant.getApi_key());
            log.info("md5:" + md5);
            if (!sign.equals(md5)) {
                result.setCode(203);
                result.setMsg("签名错误");
                result.setData("");
                return result;
            }
            notify_url = this.deleteAllHTMLTag(notify_url);
            return orderService.pay(request, out_order_no, pay_type, amount, merchant.getId(), notify_url, channel_type);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("server error");
            result.setData("");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Result query(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String merchant_no = request.getParameter("merchant_no");
        String out_order_no = request.getParameter("out_order_no");
        String sign = request.getParameter("sign");
        Result result = new Result();
        if (StringUtils.isEmpty(merchant_no)) {
            result.setCode(101);
            result.setMsg("参数缺失merchant_no");
            result.setData("");
            return result;
        }
        if (StringUtils.isEmpty(out_order_no)) {
            result.setCode(102);
            result.setMsg("参数缺失out_order_no");
            result.setData("");
            return result;
        }
        if (StringUtils.isEmpty(sign)) {
            result.setCode(108);
            result.setMsg("参数缺失sign");
            result.setData("");
            return result;
        }
        try {
            MerchantBean merchant = merchantService.getById(Long.parseLong(merchant_no));
            if (merchant == null) {
                result.setCode(201);
                result.setMsg("商户不存在");
                result.setData("");
                return result;
            }
            if (merchant.getStatus() == 0) {
                result.setCode(202);
                result.setMsg("商户被禁用");
                result.setData("");
                return result;
            }
            String signstr = merchant_no + out_order_no;
            String md5 = MD5.getMD5(signstr + merchant.getApi_key());
            if (!sign.equals(md5)) {
                result.setCode(203);
                result.setMsg("签名错误");
                result.setData("");
                return result;
            }
            OrderBean queryModel = new OrderBean();
            queryModel.setMerchant_order_no(out_order_no);
            List<OrderBean> orderList = orderService.queryList(queryModel);
            if (orderList == null || orderList.size() == 0) {
                result.setCode(204);
                result.setMsg("订单不存在");
                result.setData("");
                return result;
            }
            OrderBean order = orderList.get(0);
            Map<String, String> map = new HashMap<String, String>();
            map.put("merchant_no", order.getMerchant_id().toString());
            map.put("order_no", order.getId().toString());
            map.put("out_order_no", order.getMerchant_order_no());
            map.put("amount", order.getAmount().toString());
            map.put("pay_type", order.getPay_type());
            map.put("status", order.getStatus().toString());
            result.setCode(1);
            result.setMsg("请求成功");
            result.setData(map);
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("server error");
            result.setData("");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/orderInfo.html")
    public Result orderInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        Result result = new Result();
        if (StringUtils.isEmpty(id)) {
            result.setCode(101);
            result.setMsg("参数异常");
            result.setData("");
            return result;
        }
        try {
            OrderBean order = orderService.getById(Long.parseLong(id));
            if (order == null) {
                result.setCode(102);
                result.setMsg("订单不存在");
                result.setData("");
                return result;
            }
            Map<String, String> map = new HashMap<String, String>();
            map.put("order_id", order.getId().toString());
            map.put("out_order_no", order.getMerchant_order_no());
            map.put("amount", order.getAmount().toString());
            if (order.getStatus() == 0) {
                if ("wsgw".equals(order.getOperator()) && "alipay".equals(order.getPay_type())) {
                    map.put("pay_url", HttpUtil.getServerPath(request) + "api/zfb/" + order.getId());
                    //map.put("safe_url", HttpUtil.getServerPath(request)+ "api/safeRedirect/" + order.getId());
                    map.put("pay_param", Base64.encodeString(PassWrodsCreater.myEncrypt(order.getPay_url())));
                } else {
                    map.put("pay_url", order.getPay_url());
                }
            }
            map.put("status", order.getStatus().toString());
            map.put("pay_type", order.getPay_type());
            map.put("operator", order.getOperator());
            // TODO: 2022/12/13 测试需要删除
            map.put("expire_time", String.valueOf(order.getUpdate_time() + GlobalConfig.pay_time_out - System.currentTimeMillis() / 1000));
            // map.put("expire_time", String.valueOf(GlobalConfig.pay_time_out));
            result.setCode(1);
            result.setMsg("请求成功");
            result.setData(map);
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result.setCode(500);
            result.setMsg(e.getMessage());
            result.setData("");
            return result;
        }
    }

    @RequestMapping(value = "/payPage.html")
    public ModelAndView payPage(HttpServletRequest request, HttpServletResponse response, PrintWriter out, ModelAndView mav) throws IOException, WxErrorException {
        response.setContentType("text/html;charset=utf-8");
        String userAgent = request.getHeader("user-agent");
        if (!StringUtils.isEmpty(userAgent)) userAgent = userAgent.toLowerCase();
        log.info("user-agent:" + userAgent);
        String deviceType = "Android";
        if (userAgent.contains("macintosh") || userAgent.contains("iphone") || userAgent.contains("ipad") || userAgent.contains("ipod") || userAgent.contains("ios")) {
            deviceType = "Ios";
        }
        String id = request.getParameter("id");
        Result result = new Result();
        if (StringUtils.isEmpty(id)) {
            result.setCode(101);
            result.setMsg("订单不存在");
            result.setData("");
            out.write(JSONUtil.toJson(result));
            return null;
        }
        OrderBean order = orderService.getById(Long.parseLong(id));
        if (order == null) {
            result.setCode(102);
            result.setMsg("订单不存在");
            result.setData("");
            out.write(JSONUtil.toJson(result));
            return null;
        }
        OrderBean updateModel = new OrderBean();
        updateModel.setId(order.getId());
        updateModel.setDevice_type(deviceType);
        orderService.update(updateModel);
        long expire_time = order.getCreate_time() + GlobalConfig.pay_time_out;
        PrintWriter pw = response.getWriter();
        long current_time = System.currentTimeMillis() / 1000;
        if (expire_time < current_time) {
            pw.write("订单过期,已关闭");
            pw.flush();
            return null;
        }
        if (order.getStatus() == 1) {
            pw.write("订单支付成功,已关闭");
            pw.flush();
            return null;
        }
        mav.addObject("status", order.getStatus());
        mav.addObject("amount", order.getAmount());
        mav.addObject("order_id", order.getId());
        mav.addObject("out_order_no", order.getMerchant_order_no());
        mav.addObject("pay_type", order.getPay_type());
        mav.addObject("device_type", deviceType);
        String url = "";
        WxJsapiSignature signature = new WxJsapiSignature();
        if ("wechat".equalsIgnoreCase(order.getPay_type())) {
            try {
                url = HttpUtil.getServerPath(request) + "api/payPage.html?id=" + id;
                signature = wxMpService.createJsapiSignature(url);
            } catch (WxErrorException ex) {
                result.setCode(500);
                result.setMsg(ex.getMessage());
                out.write(JSONUtil.toJson(result));
                return null;
            }
        }
        // WxJsapiSignature signature = new WxJsapiSignature();
        // signature.setUrl("https://test");
        // signature.setAppId("123");
        // signature.setNonceStr("dddd");
        // signature.setTimestamp(10029910);
        // signature.setSignature("ieuuehdj");
        mav.addObject("signInfo", signature);
        mav.addObject("signUrl", url);
        mav.setViewName("pay");
        return mav;
    }


    @RequestMapping(value = "/payPage1.html")
    public ModelAndView payPage1(HttpServletRequest request, HttpServletResponse response, PrintWriter out, ModelAndView mav) throws IOException, WxErrorException {

        String url = HttpUtil.getServerPath(request) + "api/payPage1.html";
        WxJsapiSignature signature = wxMpService.createJsapiSignature(url);
        // WxJsapiSignature signature = new WxJsapiSignature();
        // signature.setUrl("https://test");
        // signature.setAppId("123");
        // signature.setNonceStr("dddd");
        // signature.setTimestamp(10029910);
        // signature.setSignature("ieuuehdj");
        mav.addObject("signInfo", signature);
        mav.addObject("signUrl", url);
        mav.setViewName("gzh");
        return mav;
    }


    @RequestMapping("/redirect/{id}")
    public ModelAndView redirect(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, ModelAndView mav, String type) throws IOException {
        mav.setViewName("redirect:" + orderService.getById(id).getPay_url());
        return mav;
    }

    @RequestMapping("/form/{id}")
    public void form(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, ModelAndView mav) throws IOException {
        OrderBean order = orderService.getById(id);
        response.setContentType("text/html;charset=utf-8");
        String payHtm = order.getPay_url();
        if ("shop".equals(order.getOperator()))
            payHtm = "<html><body>" + payHtm + "</body></html>";
        else if ("cloud".equals(order.getOperator()))
            payHtm = "<html><body>" + payHtm + "</body><script>document.forms[0].submit();</script></html>";
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
            pw.write(payHtm);
            pw.flush();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    @RequestMapping("/zfb/{id}")
    public ModelAndView zfb(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, ModelAndView mav, String type) throws IOException {
        OrderBean order = orderService.getById(id);
        String url = order.getPay_url();
        if ("Ios".equals(order.getDevice_type())) {
            mav.setViewName("redirect:" + this.IOS_source2Deeplink(url));
            return mav;
        }
        JSONObject json = new JSONObject();
        json.put("sourcePid", PassWrodsCreater.generateRandomNumber(4));
        json.put("external_info", url);
        json.put("pkgName", "com.alibaba.mobilei");
        json.put("session", PassWrodsCreater.generatePassword(10).toLowerCase());
        String encodedText = Base64.encodeString(json.toString());
        String payUrl = "alipays://platformapi/startapp?appId=20000125&mqpSchemePay=" + URLEncoder.encode(encodedText, "UTF-8");
        mav.setViewName("redirect:" + payUrl);
        return mav;
        //{"sourcePid":2265,"pkgName":"com.alibaba.mobilei","session":"z6urnycuyc","external_info":"alipay_sdk=alipay-sdk-java-4.8.73.ALL&app_id=2017122001013766&biz_content=%7B%22body%22%3A%22%E7%94%B5%E8%B4%B9%22%2C%22enable_pay_channels%22%3A%22balance%2CcreditCard%2CcreditCardExpress%2CcreditCardCartoon%2CdebitCardExpress%2CmoneyFund%2Ccredit_group%2Cpromotion%2Ccoupon%22%2C%22out_trade_no%22%3A%22202111200140753890%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%E7%BD%91%E4%B8%8A%E5%9B%BD%E7%BD%91%22%2C%22time_expire%22%3A%222021-11-20+17%3A26%3A04%22%2C%22total_amount%22%3A%2250.00%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=https%3A%2F%2Fpayctl.95598pay.com%2Fpayunifycontroller%2Frest%2Fnotice%2Falipay%2Fhandle&return_url=https%3A%2F%2Fsgcpay.95598pay.com%2FsgcPay%2Fpayment%2FaliPayBack.htm&sign=m91lyEA6via5phqK85oKPsAicm810ZP41Vo%2BpLYJcwf2Di6dldWc4DnHsy56Vfu93AboeD7yNeP0VFkZt1xSOyDfyImeVdwWO38MPBxVn1lJbmGqFuHYo8EVflNynU4h1oqgHlv%2FN3pMzpBpTKWusy2fR57bccSJj5zv%2BXjgpR0%2BmzD0Pn4PkCiuA6lxgXwp7Uo6E%2FH4xAhDdX7FENjHtOwAu0F2%2B7XtDGxxSNwSMsKQv1yKInennPOf%2F509LkskM%2FBM9BRgJlxDUM%2FQ9QQaCesOZ5JHMFGHXyy1TN4wLt6yQZG%2BAqv9hZhTnVQKIG2E2kjwdby981g5lvtX5HDyPQ%3D%3D&sign_type=RSA2&timestamp=2021-11-20+16%3A56%3A04&version=1.0"}
		/*long leftTime=(order.getUpdate_time()+GlobalConfig.pay_time_out)-System.currentTimeMillis()/1000;
        mav.addObject("amount", order.getAmount());
		mav.addObject("leftTime", leftTime);
        mav.addObject("orderStr", order.getPay_url());
        mav.setViewName("zfb");
       /* long expire_time=(order.getUpdate_time()+GlobalConfig.pay_time_out)-System.currentTimeMillis()/1000;
        if(expire_time<0) {
        	response.setContentType("text/html;charset=utf-8");
        	PrintWriter pw=response.getWriter();
        	pw.write("订单过期,已关闭");
			pw.flush();
			return null;
        }
        mav.addObject("expire_time", expire_time);
        mav.addObject("pay_url", payUrl);
        mav.setViewName("safePay");
        return mav;*/
    }

    @RequestMapping("/safeRedirect/{id}")
    public ModelAndView safeRedirect(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, ModelAndView mav, String type) throws IOException {
        OrderBean order = orderService.getById(id);
        String url = order.getPay_url();
        mav.addObject("pay_param", url);
        mav.setViewName("safeRedirect");
        return mav;
    }

    private String IOS_source2Deeplink(String url) throws UnsupportedEncodingException {
        JSONObject json = new JSONObject();
        json.put("requestType", "SafePay");
        json.put("dataString", url);
        json.put("fromAppUrlScheme", "com.alibaba.mobilei");
        String encodedText = URLEncoder.encode(json.toString(), "utf-8");
        return "alipaymatrixbwf0cml3://alipayclient?" + encodedText;
    }

    @ResponseBody
    @RequestMapping(value = "/sum.html")
    public String sum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OrderBean queryModel = new OrderBean();
        queryModel.setStatus(Integer.valueOf(1));
        String all = "总：" + new BigDecimal(this.orderService.sumMoney(queryModel) + "").toString();
        queryModel.setUpdate_time(DateTimeUtil.getTodayUnixTime());
        double tSum = this.orderService.sumMoney(queryModel);
        all = all + "  当日：" + new BigDecimal(tSum + "");
        queryModel.setUpdate_time(Long.valueOf(DateTimeUtil.getTodayUnixTime() - 86400L));
        double sSum = this.orderService.sumMoney(queryModel) - tSum;
        return all + "  昨日：" + new BigDecimal(sSum + "").toString();
    }

    @ResponseBody
    @RequestMapping("/receiveSms.html")
    public String receiveSms(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String phone = request.getParameter("phone");
        String content = request.getParameter("content");
        content = this.deleteAllHTMLTag(content);
        log.info(phone + ",接收到短信：" + content);
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(content)) {
            return "手机号或短信为空";
        }
        if (phone.length() != 11) {
            return "手机号码错误";
        }
        if (!content.contains("分期乐")) {
            return "不是分期乐短信";
        }
        MessageBean msg = new MessageBean();
        msg.setPhone(phone);
        msg.setContent(content);
        msg.setCreate_time(System.currentTimeMillis() / 1000);
        if (content.contains("注册")) msg.setType(1);
        else if (content.contains("登录")) msg.setType(2);
        else if (content.contains("查看卡密")) msg.setType(3);
        messageService.insert(msg);
        String verCode = "";
        Pattern pattern = Pattern.compile("\\d{6}");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            verCode = matcher.group();
            if (msg.getType() == 3) {
                orderService.updateGetCdkSmsBySendTime(phone, verCode);
            }
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/testPay.html")
    public Result testPay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        Result result = new Result();
        String key = request.getParameter("key");
        /*if(StringUtils.isEmpty(key)||!key.equals(UserController.key))
        {
        	result.setCode(0);
            result.setMsg("非法请求");
            result.setData("");
            return result;
        }*/
        MerchantBean merchant = merchantService.getById(10000l);
        if (merchant == null) {
            result.setCode(0);
            result.setMsg("测试商户不存在");
            result.setData("");
            return result;
        }
        String merchant_no = String.valueOf(merchant.getId());
        String out_order_no = "" + System.currentTimeMillis() / 1000;
        String amount = request.getParameter("amount");
        String channel_type = request.getParameter("channel_type");
        String pay_type = request.getParameter("pay_type");
        String notify_url = HttpUtil.getServerPath(request) + "api/testNotifyUrl.html";
        if (!StringUtils.hasLength(channel_type)) {
            channel_type = "weidian";
        }
        String sign = MD5.getMD5(merchant_no + out_order_no + amount + channel_type + pay_type + notify_url + merchant.getApi_key());
        Map<String, String> param = new HashMap<String, String>();
        param.put("merchant_no", merchant_no);
        param.put("out_order_no", out_order_no);
        param.put("amount", amount);
        param.put("pay_type", pay_type);
        param.put("channel_type", channel_type);
        param.put("notify_url", notify_url);
        param.put("sign", sign);
        Response res = Http.create(HttpUtil.getServerPath(request) + "api/pay").body(param).requestType(Http.RequestType.FORM).post().send().getResponse();
        if (StringUtils.isEmpty(res.getResult())) {
            result.setCode(0);
            result.setMsg("网络异常");
            result.setData("");
            return result;
        }
        JSONObject json = JSONObject.fromObject(res.getResult());
        if (json.getInt("code") == 1) {
            result.setCode(json.getInt("code"));
            result.setMsg(json.getString("msg"));
            result.setData(json.getJSONObject("data").getString("pay_url"));
            return result;
        }
        result.setCode(json.getInt("code"));
        result.setMsg(json.getString("msg"));
        result.setData("");
        return result;
    }

    @RequestMapping(value = "/testPayPage.html")
    public ModelAndView testPayPage(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, String pay_type) throws IOException {
        mav.addObject("key", UserController.key);
        mav.setViewName("testPay");
        return mav;
    }

    // @RequestMapping(value = "/gzh.html")
    // public ModelAndView gzh(HttpServletRequest request,HttpServletResponse response,ModelAndView mav,String pay_type) throws IOException, WxErrorException {
    // 	String url=HttpUtil.getServerPath(request)+"/api/gzh.html";
    // 	WxJsapiSignature result = wxMpService.createJsapiSignature(url);
    // 	mav.addObject("signInfo", result);
    // 	mav.setViewName("gzh");
    // 	return mav;
    // }

    @ResponseBody
    @RequestMapping(value = "/testNotifyUrl.html")
    public String testNotifyUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "success";
    }

    @RequestMapping(value = "/test.html")
    public ModelAndView test(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) throws IOException {
        mav.setViewName("test");
        return mav;
    }

    @RequestMapping(value = "/qrImage.jpg")
    public void qrImage(HttpServletRequest request, HttpServletResponse response, String qr) throws WriterException, IOException {
        if (qr == null) return;
        QrCodeUtil.responseQrCode(qr, 400, "jpg", response);
    }

    @RequestMapping(value = "/orderMatchingPage.html")
    public ModelAndView aliPayMatching(HttpServletRequest request, HttpServletResponse response, PrintWriter out, ModelAndView mav) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        Result result = new Result();
        try {
            if (StringUtils.isEmpty(id)) {
                result.setCode(101);
                result.setMsg("订单不存在");
                result.setData("");
                out.write(JSONUtil.toJson(result));
                return null;
            }
            OrderBean order = orderService.getById(Long.parseLong(id));
            if (order == null) {
                result.setCode(102);
                result.setMsg("订单不存在");
                result.setData("");
                out.write(JSONUtil.toJson(result));
                return null;
            }
            mav.addObject("id", id);
            mav.addObject("leftTime", 20);
            mav.setViewName("matching");
            return mav;
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg(e.getMessage());
            result.setData("");
            out.write(JSONUtil.toJson(result));
            return null;
        }
    }

}
